package com.huike.refreslistview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class RefreshListView extends ListView {


    private ProgressBar mPbMain;
    private ImageView mIvMain;
    private TextView mTvMain;
    private View inflate;//视图
    private int height;//测量过后头部高度


    //状态
    private static final int START_PULL_TO_REFRESH = 0;//下拉刷新状态
    private static final int START_RELEASE_TO_REFRESH = 1;//即将刷新状态
    private static final int START_REFRESHING = 2;//正在刷新

    private int mCurrentState = START_PULL_TO_REFRESH;//初始状态 = 下拉状态
    private RotateAnimation aniUp;
    private RotateAnimation aniDow;


    public RefreshListView(Context context) {
        this(context,null);
    }

    public RefreshListView(Context context, AttributeSet attrs) {
        this(context, attrs,-1);
    }

    public RefreshListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        initHeard(context);

    }

    private void initHeard(Context context) {

        //给listview 添加头布局
        inflate = View.inflate(context, R.layout.header_refersh, null);

        mPbMain = (ProgressBar) inflate.findViewById(R.id.pb_main);
        mIvMain = (ImageView) inflate.findViewById(R.id.iv_main);
        mTvMain = (TextView) inflate.findViewById(R.id.tv_main);

        addHeaderView(inflate);

        //测量头部高度 将padding 设置为负 有隐藏布局
        inflate.measure(0,0);
        height = inflate.getMeasuredHeight();
        inflate.setPadding(0,-height,0,0);

        initArrowAnim();

    }

    private int startY = -1;

    @Override
    public boolean onTouchEvent(MotionEvent ev) {

        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                 startY = (int) ev.getY();
                break;


            case MotionEvent.ACTION_MOVE:
                int end = (int) ev.getY();
                int dy = end -startY; //得到偏移量

                //如果是正在刷新的状态直接跳出 ACTION_MOVE
                if(mCurrentState == START_REFRESHING){
                    break;
                }


//                判断listView 在顶部时 下拉才有效果
                int firstVisiblePosition = this.getFirstVisiblePosition();//listView 第一个item位置
                if(dy >0 && firstVisiblePosition == 0 ){
                    int padding = -height + dy;//下拉多少 显示多少头部

//                    刷新状态
                    if(padding > 0 && mCurrentState != START_RELEASE_TO_REFRESH ){
                        mCurrentState =START_RELEASE_TO_REFRESH;
//                        显示状态

                        ReFreshState();


                    }else if(padding <= 0 && mCurrentState != START_PULL_TO_REFRESH){
                        mCurrentState = START_PULL_TO_REFRESH;
                        ReFreshState();

                    }


                    //修改 padding 位置
                    inflate.setPadding(0,padding,0,0);
                    return true;
                }


                break;

            case MotionEvent.ACTION_UP:
                startY = -1; //初始化 开始位置

                if(mCurrentState ==  START_RELEASE_TO_REFRESH){
                    mCurrentState  = START_REFRESHING; //将状态修改为 正在刷新

                    //在显示位置刷新
                    inflate.setPadding(0,0,0,0);

                    //显示状态
                    ReFreshState();

                }else if(mCurrentState == START_PULL_TO_REFRESH){ //松开手时 状态为下拉刷新（就是下拉了一点点就松开了） 直接隐藏头部
                    //隐藏下拉刷新状态

                    inflate.setPadding(0,-height,0,0);

                    mCurrentState  = START_PULL_TO_REFRESH;

                    ReFreshState();
                }


                break;
        }

        return super.onTouchEvent(ev);
    }

    private void initArrowAnim(){
        aniUp = new RotateAnimation(0,-180, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);

        aniDow = new RotateAnimation(-180,0,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);

        aniUp.setFillAfter(true);
        aniDow.setFillAfter(true);

        aniUp.setDuration(300);
        aniDow.setDuration(300);


    }

    private void ReFreshState() {

        switch (mCurrentState){

            case START_PULL_TO_REFRESH:
                mIvMain.setImageResource(R.drawable.rjd_down);
                mIvMain.startAnimation(aniDow);
                mTvMain.setText("下拉刷新");

                break;
            case START_RELEASE_TO_REFRESH:
                mIvMain.startAnimation(aniUp);
                mPbMain.setVisibility(INVISIBLE);
                mTvMain.setText("即将刷新");

                break;
            case START_REFRESHING:
                //当图片有动画状态时 必须要先清楚动画 才能隐藏图片
                mIvMain.clearAnimation();
                mPbMain.setVisibility(VISIBLE);
                mIvMain.setVisibility(INVISIBLE);
                mTvMain.setText("正在刷新");

                if(listener != null){
                    listener.onRefresh();
                }

                break;

        }

    }

    public void setComplenterListener(){

//        测量头部高度 将padding 设置为负 有隐藏布局
        inflate.measure(0,0);
        height = inflate.getMeasuredHeight();
        inflate.setPadding(0,-height,0,0);


        mIvMain.setVisibility(VISIBLE);
        mIvMain.setImageResource(R.drawable.rjd_down);
        mPbMain.setVisibility(INVISIBLE);
        mTvMain.setText("下拉刷新");
        mCurrentState = START_PULL_TO_REFRESH;

    }


    private onRefreshListener listener;
    public void setonRefreshListener(onRefreshListener listener){
        this.listener = listener;
    }

    public interface onRefreshListener{
        void onRefresh();
    }

}